package com.umeox.watch.moto.dataservice.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.dataservice.base.BaseApp;
import com.umeox.watch.moto.dataservice.base.LauncherManager;
import com.umeox.watch.moto.dataservice.services.ApiService;
import com.umeox.watch.moto.dataservice.utils.LogUtils;

import java.util.TimeZone;

public class DateTimeReceiver extends BroadcastReceiver {
    private static final String ACTION_TIMEZONE_CHANGED = Intent.ACTION_TIMEZONE_CHANGED;
    private static final String ACTION_TIME_CHANGED = Intent.ACTION_TIME_CHANGED;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (ACTION_TIMEZONE_CHANGED.equals(action)) {
            Logger.d("---TIMEZONE_CHANGED---");
            LogUtils.saveLog("---TIMEZONE_CHANGED---");
            String id = intent.getStringExtra("time-zone");
            if(TextUtils.isEmpty(id)){
                return;
            }
            TimeZone newTimeZone = TimeZone.getTimeZone(intent.getStringExtra("time-zone"));
            Logger.d("timezone changed: id: " + newTimeZone.getID() + " offset: " + newTimeZone.getRawOffset());
            LauncherManager.getInstance().resetAlarm();
            //通知服务器时区改变
            ApiService.updateTimeZone(BaseApp.getInstance().deviceId, newTimeZone.getID());
        } else if (ACTION_TIME_CHANGED.equals(action)) {
            Logger.d("---TIME_CHANGED---");
            //BaseApp.getInstance().resetAlarm();
        }
    }
}
